<template>
  <div>
    <el-form
      :inline="true"
      :model="cForm"
      :disabled="!isShowList"
    >
      <el-form-item label="一级分类" >
        <!-- select v-model收集的是选中的option的value的值 -->
        <el-select
          v-model="cForm.category1Id"
          placeholder="请选择"
          @change="handlerCategroy1"
        >
          <el-option
            v-for="c1 in category1List"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <!-- select v-model收集的是选中的option的value的值 -->
        <el-select
          v-model="cForm.category2Id"
          placeholder="请选择"
          @change="handlerCategroy2"
        >
          <el-option
            v-for="c2 in category2List"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <!-- select v-model收集的是选中的option的value的值 -->
        <el-select
          v-model="cForm.category3Id"
          placeholder="请选择"
          @change="handlerCategroy3"
        >
          <el-option
            v-for="c3 in category3List"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "CategorySelector",
  props:['isShowList'],
  data() {
    return {
      category1List: [],
      category2List: [],
      category3List: [],
      cForm: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
      },
    };
  },
  mounted() {
    this.getCategory1List();
  },
  methods: {
    async getCategory1List() {
      try {
        const result = await this.$API.category.getCategory1();
        if (result.code === 200 || result.code === 20000) {
          this.category1List = result.data;
        }
      } catch (error) {
        this.$message.error("获取1级分类失败" + error.message);
      }
    },
    // 选中1级的时候回调,请求获取2级分类
    async handlerCategroy1(category1Id) {
      // 重新选择一级先清空二三级对应的数据
      this.cForm.category2Id = "";
      this.cForm.category3Id = "";
      this.category2List = [];
      this.category3List = [];

      this.$emit("changeCategory", { categoryId: category1Id, level: 1 });
      try {
        const result = await this.$API.category.getCategory2(category1Id);
        if (result.code === 200 || result.code === 20000) {
          this.category2List = result.data;
        }
      } catch (error) {
        this.$message.error("获取2级分类失败" + error.message);
      }
    },
    // 选中2级的时候回调 请求获取3级分类
    async handlerCategroy2(category2Id) {
      // 重新选择二级 清空三级
      this.cForm.category3Id = "";
      this.category3List = [];
      this.$emit("changeCategory", { categoryId: category2Id, level: 2 });

      try {
        const result = await this.$API.category.getCategory3(category2Id);
        if (result.code === 200 || result.code === 20000) {
          this.category3List = result.data;
        }
      } catch (error) {
        this.$message.error("获取3级分类失败" + error.message);
      }
    },
    handlerCategroy3(category3Id) {
      this.$emit("changeCategory", { categoryId: category3Id, level: 3 });
    },
  },
};
</script>

<style lang="less" scoped>
</style>
